/**
 * @overview 添加 实时billBoard
 * @author dengqiang
 * @description 引用鲸云 CgsPoint 类 和Cesium CallbackProperty
 * @update  2022.04.15
 */
class GModelMoving {
  constructor() {
    this.carPrePosition = new Map()
  }

  //新增单个人员
  addedBySocket(paramObj) {
    const that = this
    const { name, parentName, option } = paramObj
    const viewer = window.map3DControl.viewer
    // let newData = option.peopleLocationData;//后台传的数据，格式已确定。
    const peopleObj = that.searchObject(window.GPointArray, option.userId)
    const prePos = {
      longitude: option.longitude,
      latitude: option.latitude,
      height: option.height,
    }
    //这里需要判断传入得人员信息是否已经创建过了
    if (peopleObj) {
      //修改已创建人的坐标
      //上一个点位置
      let prePosition = that.carPrePosition.get(option.userId) //  甘肃项目不需要进行转化

      //检查是否有上一个点的信息
      if (prePosition == undefined) {
        prePosition = prePos
      }

      //保存上个一点信息
      that.carPrePosition.set(option.userId, prePos)

      // 确保地形挡住Polygon
      viewer.scene.globe.depthTestAgainstTerrain = true

      // 确保动画
      viewer.clock.shouldAnimate = true
      viewer.clock.clockRange = Cesium.ClockRange.CLAMPED //Loop at the end?

      // 时间设置
      const start = viewer.clock.currentTime
      viewer.clock.multiplier = 1
      const minHeight = 1
      const maxHeight = 300
      const time = option.duration || 2 //持续时间
      const animationStop = Cesium.JulianDate.addSeconds(
        start,
        Number.MAX_SAFE_INTEGER,
        new Cesium.JulianDate()
      )
      const stretchHeightProperty = new Cesium.SampledProperty(Number)
      stretchHeightProperty.addSample(animationStop, maxHeight)
      stretchHeightProperty.addSample(start, minHeight)
      stretchHeightProperty.addSample(
        Cesium.JulianDate.addSeconds(start, time, new Cesium.JulianDate()),
        maxHeight
      )

      //小车动态移动，实时更改坐标点
      const curPosition = new Cesium.CallbackProperty(function (time, result) {
        const k = stretchHeightProperty.getValue(time) / (maxHeight - minHeight)
        const x = prePosition.longitude + (option.longitude - prePosition.longitude) * k
        const y = prePosition.latitude + (option.latitude - prePosition.latitude) * k
        const z = prePosition.height + (option.height - prePosition.height) * k
        const currentPosition = Cesium.Cartesian3.fromDegrees(x, y, z)
        //标牌需要在里面指定坐标
        // peopleObj.point.feature2D.coordinate = [x, y, z];
        // peopleObj.point.setPosition([x, y, z]);
        return currentPosition
      })

      //给人员坐标赋值并显示出来
      // peopleObj.point.feature3D.entity.show = true;
      peopleObj.point.feature3D.entity.position = curPosition
    } else {
      //创建新的人员
      window.GPoint.add({
        name: name,
        parentName: parentName,
        option: {
          position: [option.longitude, option.latitude, option.height], //这里的高度需要后台返回。根据option.floor信息，向后台请求离地高度，这里的height = 楼层离地高度 + BIM海拔高度
          name: '',
          attribute: option.attribute,
          // url:require('../../../src/assets/img/Gansu/Huagong/AreaList/消防员.png'),
          url: option.imgUrl,
          scale: option.scale,
        },
      })

      //保存上个一点信息，用来计算。
      that.carPrePosition.set(option.userId, prePos)
    }
  }

  //根据id从数组中查找对象
  searchObject(array, id) {
    let result = 0
    array.forEach((ele) => {
      if (ele.name == id) {
        result = ele
      }
    })
    return result
  }

  //根据name移除单个人员
  removeByName(name) {
    window.GPointArray.forEach((item, index) => {
      if (item.name == name) {
        window.map.removeLayer(item.point)
        window.GPointArray.splice(index, 1)
      }
    })
  }

  //根据parentName移除一批人员
  removeByPatName(parentName) {
    const length = window.GPointArray.length
    for (let i = length - 1; i >= 0; i--) {
      if (window.GPointArray[i].parentName == parentName) {
        window.map.removeLayer(window.GPointArray[i].point)
        window.GPointArray.splice(i, 1)
      }
    }
  }

  //根据name控制单个人员的显隐
  setVisibleByName(name, boolean) {
    window.GPointArray.forEach((item) => {
      if (item.name == name) {
        item.point.setVisible(boolean)
      }
    })
  }

  //根据parentName控制一批人员的显隐
  setVisibleByPatName(parentName, boolean) {
    window.GPointArray.forEach((item) => {
      if (item.parentName == parentName) {
        item.point.setVisible(boolean)
      }
    })
  }

  //根据userID删除上一个位置信息
  removePrePosition(id) {
    this.carPrePosition.delete(id)
  }
}

export default GModelMoving
